﻿using System;
using System.Data;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Openfree.Framework.DataAccess;

public partial class PMSWeb_CostCharge : PMSBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.TableName = "PMS_CostBill";
        this.Key = "BillID";
        if (!IsPostBack)
        {
            pnlGrid.Visible = true;
            pnlEdit.Visible = false;
//            string cmdText = @"select a.*,
//                (select count(*) from PMS_CostBillItem where BillID=a.BillID) UserCount,
//                (select sum(PayMoney) from PMS_CostBillItem where BillID=a.BillID and PayMoney>=0  and IsSelected=1) ShouFei,
//                (select sum(PayMoney) from PMS_CostBillItem where BillID=a.BillID and PayMoney<0  and IsSelected=1) QianFei
//                from PMS_CostBill a order by BillDate desc";
//            FillGirdView(0, cmdText);
        }
        else
        {
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
        }
    }
   
    protected void FillGirdView(int currentPage, string cmdText)
    {
        if (string.IsNullOrEmpty(cmdText))
        {
            if (ViewState[this.ClientID + "cmdText"] == null) return;
            cmdText = ViewState[this.ClientID + "cmdText"].ToString();
        }
        SqlSplitPage splitPage = new SqlSplitPage(base.Connection, cmdText);
        splitPage.PageSize = 10;
        DataSet ds = splitPage.LoadDataSet(currentPage);
        //成功查询出数据后缓存
        ViewState[this.ClientID + "cmdText"] = cmdText;
        gridView.ShowFooter = true;
        gridView.NavigationData = splitPage.GetNavigationData(currentPage);
        gridView.DataSource = ds.Tables[0];
        gridView.DataBind();
    }

    protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        { 
            //e.Row.Cells[5].Text = FormatBoolean(e.Row.Cells[5].Text, "是", "否");
            //e.Row.Cells[6].Text = FormatBoolean(e.Row.Cells[6].Text, "是", "否");
            //e.Row.Cells[7].Text = FormatBoolean(e.Row.Cells[7].Text, "是", "否");
        }
    }

    protected void gridView_SplitPageIndexChange(object sender, CommandEventArgs e)
    {
        FillGirdView(Int32.Parse(e.CommandArgument.ToString()), ViewState[this.ClientID + "cmdText"].ToString());
    }

    protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        hfBillID.Value = e.CommandArgument.ToString();
        EditDate();
    }

    private void ResetComponent()
    {
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string cmdText = @"select a.*,
                (select count(*) from PMS_CostBillItem where BillID=a.BillID) UserCount,
                (select sum(PayMoney) from PMS_CostBillItem where BillID=a.BillID and PayMoney>=0  and IsSelected=1) ShouFei,
                (select sum(PayMoney) from PMS_CostBillItem where BillID=a.BillID and PayMoney<0  and IsSelected=1) QianFei
                from PMS_CostBill a where a.BillID in 
                (
                select BillID from PMS_CostBillItem 
                where HouseNO='{0}' and RoomNo='{1}'
                group by BillID 
                )
                order by BillDate desc";

        cmdText = string.Format(cmdText, tbHouseNo.Text, tbRoomNo.Text);
        FillGirdView(0, cmdText);
    }


    protected void btnEdit_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Request["BillID"]))
        {
            base.Alert("请选择一条记录");
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
            return;
        }
        hfBillID.Value = Request["BillID"];
        EditDate();
    }

    private void EditDate()
    {
        ResetComponent();
        hfAddOrEdit.Value = "Edit";
        pnlGrid.Visible = false;
        pnlEdit.Visible = true;
        DataSet ds = PMSAccess.GetDataSet(this.TableName, this.Key, hfBillID.Value);
        DataRow rowCostBill = ds.Tables[0].Rows[0];

        lbSubject.Text = rowCostBill["Subject"].ToString();
        lbBillDate.Text = DateTime.Parse(rowCostBill["BillDate"].ToString()).ToString("yyyy-MM-dd");
        lbHouseNo.Text = tbHouseNo.Text;
        lbRoomNo.Text = tbRoomNo.Text;

        string cmdText = @"select * from (select a.*, 
                (select OrderIndex from PMS_CostItem where CostID=a.CostID) OrderIndex,
                (select Subject from PMS_CostItem where CostID=a.CostID) Subject
                from PMS_CostBillItem a where BillID='{0}' and HouseNo='{1}' and RoomNo='{2}' and IsSelected=1) z order by OrderIndex";
        cmdText = string.Format(cmdText, hfBillID.Value, tbHouseNo.Text, tbRoomNo.Text);
        ds = BaseAccess.GetDataSet(base.Connection, cmdText, "PMS_CostBillItem");
        lbTelephone.Text = ds.Tables[0].Rows[0]["Telephone"].ToString();

        string html = string.Empty;
        string money = string.Empty;
        string payMoney = string.Empty;
        foreach (DataRow row in ds.Tables[0].Rows)
        {

            if (string.IsNullOrEmpty(row["PayMoney"].ToString()))
            {
                payMoney = "";
                if (!string.IsNullOrEmpty(row["Money"].ToString()) &&
                    double.Parse(row["Money"].ToString()) != 0)
                {
                    payMoney = double.Parse(row["Money"].ToString()).ToString("###,###,###.00");
                }
            }
            else
            {
                payMoney = double.Parse(row["PayMoney"].ToString()).ToString("###,###,###.00"); ;
            }
            

            html += string.Format("<tr><td>{0}</td><td colspan=\"3\">"+
                "<input id=\"{1}\" name=\"{1}\" type=\"text\" value=\"{2}\" style=\"width:100px\" />[Money]</td></tr>",
                row["Subject"].ToString(), "tb"+ row["CostID"].ToString(), payMoney);

            if (string.IsNullOrEmpty(row["Money"].ToString()) ||
                double.Parse(row["Money"].ToString()) == 0)
            {
                html = html.Replace("[Money]", "");
            }
            else
            {
                html = html.Replace("[Money]", " ("+double.Parse(row["Money"].ToString()).ToString("###,###,###.00")+"￥)");
            }
        }

        lbCostItem.Text = html;
    }


    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnlGrid.Visible = true;
        pnlEdit.Visible = false;
        FillGirdView(this.gridView.SplitPageIndex, string.Empty);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string cmdText = @"select * from PMS_CostBillItem a
            where BillID='{0}' and HouseNo='{1}' and RoomNo='{2}' and IsSelected=1";
        cmdText = string.Format(cmdText, hfBillID.Value, tbHouseNo.Text, tbRoomNo.Text);
        DataSet ds = BaseAccess.GetDataSet(base.Connection, cmdText, "PMS_CostBillItem");

        string payMoney = string.Empty;
        string result = string.Empty;
        foreach (DataRow row in ds.Tables[0].Rows)
        {
            payMoney = Request["tb" + row["CostID"].ToString()];
            if (string.IsNullOrEmpty(payMoney))
            {
                row["PayMoney"] = DBNull.Value;
                row["PayDate"] = DateTime.Now;
            }
            else
            {
                double value = 0;
                if (double.TryParse(payMoney, out value))
                {
                    row["PayMoney"] = value;
                    row["PayDate"] = DateTime.Now;
                }
                else
                {
                    result = BaseAccess.ExecuteScalar(base.Connection, 
                        "select Subject from PMS_CostItem where CostID='"+ row["CostID"].ToString()+"'");
                }
            }
        }


        if (!string.IsNullOrEmpty(result))
        {
            base.Alert(result + "输入错误,请输入半角数值类型");
            return;
        }

        BaseAccess.UpdateDataSet(base.Connection, "PMS_CostBillItem", ds);

        btnCancel_Click(sender, e);
    }

    private void ExecuteEdit()
    {
        //DataSet ds = PMSAccess.GetDataSet(this.TableName, this.Key, hfCostID.Value);
        //DataRow row = ds.Tables[0].Rows[0];
        //row["Cycle"] = tbCycle.Text;
        //row["Description"] = tbDescription.Text;
        //row["Money"] = tbMoney.Text.Trim() == string.Empty ? 0 : float.Parse(tbMoney.Text.Trim());
        //row["Subject"] = tbSubject.Text;
        //row["OrderIndex"] = tbOrderIndex.Text;
        //row["IsDeleted"] = rbIsDeleted.SelectedValue;
        //row["IsSelected"] = rbIsSelected.SelectedValue;
        //row["IsUsed"] = rbIsUsed.SelectedValue;
        //PMSAccess.UpdateDataSet(this.TableName, ds.GetChanges());
    }
}
